package com.jianguozh.difs.support.api.ops;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.jianguozh.difs.common.bean.R;
import com.jianguozh.difs.framework.base.BaseController;
import com.jianguozh.difs.support.service.utils.EnumUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 字典 前端控制器
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/4/8 17:38
 */
@RestController
@RequestMapping("/dict")
public class DictController extends BaseController {

    @Autowired
    MybatisPlusProperties mybatisPlusProperties;

    /**
     * 由key获取对象
     */
    @GetMapping("/{key}")
    public R<List<Dict>> get(@PathVariable String key) {
        String packageName = mybatisPlusProperties.getTypeEnumsPackage();
        Map<String, Map> map = EnumUtils.getEnums(packageName);
        List result = new ArrayList();
        if (null != map && map.size() > 0) {
            Map items = map.get(key);
            if (null != items && items.size() > 0) {
                Map list = (Map) items.get("list");
                list.forEach((k, v) -> {
                    Dict dict = new Dict((String) v, (String) k);
                    result.add(dict);
                });
            }
        }
        return success(result);

    }

    /**
     * 获取所有对象
     */
    @GetMapping("/all")
    public R<Map> getAll() {
        String packageName = mybatisPlusProperties.getTypeEnumsPackage();
        Map map = EnumUtils.getEnums(packageName);
        return success(map);
    }

    //TODO: 合并持久化数据字典
    @Data
    @AllArgsConstructor
    public class Dict {
        private String label;
        private String value;
    }
}
